﻿@model IEnumerable<Backend.JYD.Model.Task.TaskBaseInfo>
@using Backend.JYD.Common;
@using Backend.JYD.Model;
@using Backend.JYD.Model.Task;
@using Backend.JYD.Model.Cust;
@using Backend.JYD.Business;
@using Backend.JYD.Model.Auth;
@{
    ViewBag.Title = "我的任务";
    ViewBag.CurrMenu = "mu0201";
    var stepModel = ViewBag.TaskStepModeles as List<TaskStepModel>;
    var modeles = this.ViewBag.TaskModels as List<TaskStepModel>;
}

<div class="frame-tit">
    <span class="ico-left"></span>
    <div class="title">@ViewBag.Title</div>
    <span class="ico-right"></span>
</div>
<div class="frame-cont">
    <h2>查询条件</h2>
    <table>
        <tr>
            <td>录入时间：</td>
            <td>
                <input  type="text" name="txtStartDate" id="txtStartDate" value="@ViewBag.StartDate"  onfocus="WdatePicker()" readonly="readonly" />
                <img class="hand" onclick="WdatePicker({el:'txtStartDate'})" src="/skin/datepicker/skin/datePicker.gif" width="16" height="22" align="absmiddle" />
                至
                <input  type="text" name="txtEndDate" id="txtEndDate" value="@ViewBag.EndDate"  onfocus="WdatePicker()" readonly="readonly" />
                <img class="hand" onclick="WdatePicker({el:'txtEndDate'})" src="/skin/datepicker/skin/datePicker.gif" width="16" height="22" align="absmiddle" />
            </td>
        </tr>
        <tr>
            <td>任务状态：</td>
            <td>
                <select name="ddlStatus" id="ddlStatus">
                    <option value="">全部</option>
                    <option value="1" @(ViewBag.Status == "1" ? "selected=\"selected\"" : string.Empty)>未提交</option>
                    <option value="2" @(ViewBag.Status == "2" ? "selected=\"selected\"" : string.Empty)>已提交</option>
                </select>
                &nbsp;&nbsp;
                @{
                    if (modeles != null)
                    {
                <label>任务步骤：</label>
                <select name="ddlCode" id="ddlCode">
                    <option value="">全部</option>
                    @foreach (var step in modeles)
                    {
                        <option value="@step.StepId" @(ViewBag.Code == step.StepId.ToString() ? "selected=\"selected\"" : string.Empty)>@step.StepName</option>
                    }
                </select>
                    }
                }
                @*<label for="oldtask"><input type="checkbox" value="1" id="oldtask" @(ViewBag.Old as string == "1" ? "checked=\"checked\"" : string.Empty)/>历史任务</label>*@
            </td>
            <td>
                <input type="button" onclick="loadpage();" value="查询"/>
                @Html.Hidden("hidPageIndex", ViewBag.PageIndex as string)
            </td>
        </tr>
    </table>
<p>

</p>
<h2>我的任务列表</h2>
      @*@if (stepModel != null && Model != null)
      {
          if (stepModel.Exists(p => p.StepCode.ToLower() == TaskManager.StepModelCode.HandOut.ToString().ToLower()))
          {
            <input type="button" value="分派" title="分派任务" onclick="handoutAll(this);"/>
          }
          if (stepModel.Exists(p => p.StepCode.ToLower() != TaskManager.StepModelCode.Marketing.ToString().ToLower() 
              && p.StepCode.ToLower() != TaskManager.StepModelCode.HandOut.ToString().ToLower() )
              && Model.ToList().Exists(p1 => p1.Status == (int)TaskManager.TaskStatus.Beginning))
          {
            <input type="button" value="开始处理" title="开始处理" onclick="processAll('@TaskManager.TaskStatus.Processing.ToString().ToLower()');"/>
          }
          if (stepModel.Exists(p => p.StepCode.ToLower() != TaskManager.StepModelCode.Marketing.ToString().ToLower() && p.StepCode.ToLower() != TaskManager.StepModelCode.HandOut.ToString().ToLower() && !string.IsNullOrWhiteSpace(p.LastStepIds) && Model.ToList().Exists(p1 => p1.Status == (int)TaskManager.TaskStatus.Processing)))
          {
            <input type="button" value="返回" title="当前未执行完成，返回到前面步骤" onclick="processAll('@TaskManager.TaskStatus.ReturnToLast.ToString().ToLower()');"/>
          }
          if (stepModel.Exists(p => p.StepCode.ToLower() != TaskManager.StepModelCode.Marketing.ToString().ToLower() && p.StepCode.ToLower() != TaskManager.StepModelCode.HandOut.ToString().ToLower() && p.AllowLoop && Model.ToList().Exists(p1 => p1.Status == (int)TaskManager.TaskStatus.Processing)))
          {
            <input type="button" value="循环步骤" title="循环步骤" onclick="processAll('@TaskManager.TaskStatus.CompleteAndLoop.ToString().ToLower()');"/>
          }
          if (stepModel.Exists(p => p.StepCode.ToLower() != TaskManager.StepModelCode.Marketing.ToString().ToLower() && p.StepCode.ToLower() != TaskManager.StepModelCode.HandOut.ToString().ToLower() && !string.IsNullOrWhiteSpace(p.NextStepIds) && Model.ToList().Exists(p1 => p1.Status == (int)TaskManager.TaskStatus.Processing)))
          {
            <input type="button" value="提交" title="当前任务完成，提交到下一步" onclick="processAll('@TaskManager.TaskStatus.CompleteAndReady.ToString().ToLower()');"/>
          }
          if (stepModel.Exists(p => p.StepCode.ToLower() != TaskManager.StepModelCode.Marketing.ToString().ToLower() && p.StepCode.ToLower() != TaskManager.StepModelCode.HandOut.ToString().ToLower() && p.AllowStop && Model.ToList().Exists(p1 => p1.Status == (int)TaskManager.TaskStatus.Processing)))
          {
            <input type="button" value="结束/终止" title="任务结束，任务终止" onclick="processAll('@TaskManager.TaskStatus.Complete.ToString().ToLower()');"/>
          }
      }*@
    <table class="table1" width="100%" border="0" cellpadding="0" cellspacing="1">
        <thead>
            <tr>
                <th width="20px"><div class="th1"></div></th>
                <th width="20px"><div class="th1"><input type="checkbox" value="1" onclick="checkAll(this);"/></div></th>
                <th width="50px"><div class="th1">任务编码</div></th>
                <th width="20px"><div class="th1">步骤号</div></th>
                <th width="60px"><div class="th1">步骤名</div></th>
                <th width="60px"><div class="th1">客户号</div></th>
                <th width="60px"><div class="th1">客户姓名</div></th>
                <th width="60px"><div class="th1">电话</div></th>
                <th width="60px"><div class="th1">执行角色</div></th>
                <th width="60px"><div class="th1">执行人</div></th>
                <th width="60px"><div class="th1">状态</div></th>
                <th width="60px"><div class="th1">创建时间</div></th>
                <th width="60px"><div class="th1">修改时间</div></th>
                <th width="60px"><div class="th1">操作人</div></th>
                <th width="100px"><div class="th1">操作</div></th>
            </tr>
        </thead>
        <tbody>
        @if (Model != null)
        {
            int idx = 0;
            var empInfos = this.ViewBag.EmpInfos as List<EmpInfo>;
            var roleInfos = this.ViewBag.RoleInfos as List<RoleInfo>;
            var auth = ViewBag.LoginAuthorityInfoes as List<AuthorityInfo>;
            foreach (var item in Model)
            {
                TaskStepModel step = stepModel == null ? null : stepModel.Find(p => p.StepId == item.StepId && p.TaskModeId == item.TaskModelId);

                string name = "chk" + item.TaskDetailId;
                string empNames = string.Empty;
                string roleNames = string.Empty;
                string opName = string.Empty;
                if (empInfos != null)
                {
                    foreach (var info in empInfos)
                    {
                        if (("," + item.EmpIds + ",").Contains("," + info.EmpId + ","))
                        {
                            empNames += info.EmpName + ",";
                        }
                    }
                    empNames = empNames.Trim(',');
                    var opInfo = empInfos.Find(p => p.EmpId == item.OpId);
                    if (opInfo != null)
                    {
                        opName = opInfo.EmpName;
                    }
                }
                if (roleInfos != null)
                {
                    foreach (var info in roleInfos)
                    {
                        if (("," + item.RoleIds + ",").Contains("," + info.RoleId + ","))
                        {
                            roleNames += info.RoleName + ",";
                        }
                    }
                    roleNames = roleNames.Trim(',');
                }
                idx++;
                
            <tr>
                <td model="@(step == null ? string.Empty : step.StepCode.ToLower())" status="@item.Status">@idx</td>
                <td>
                    @if ((item.Status == (int)TaskManager.TaskStatus.Beginning || item.Status == (int)TaskManager.TaskStatus.Processing)
                        && step != null && step.StepCode.ToLower() != TaskManager.StepModelCode.Marketing.ToString().ToLower())
                    {
                    <input type="checkbox" value="@item.TaskId" name="@name"/>
                    }
                </td>
                <td>@(item.TaskId <= 0 ? "-" : item.TaskId.ToString())</td>
                <td>@(item.StepId <= 0 ? "-" : item.StepId.ToString())</td>
                <td>@(step == null ? string.Empty : step.StepName)</td>
                <td>@item.CustNo</td>
                <td>@item.CustName</td>
                <td>@item.Phone</td>
                <td>@roleNames</td>
                <td>@empNames</td>
                <td>@(TaskManager.GetTaskStatusCode(item.Status))</td>
                <td>@String.Format("{0:g}", item.CreateTime)</td>
                <td>@String.Format("{0:g}", item.UpdateTime)</td>
                <td>@opName</td>
                 <td>
                    <input type="button" value="查看" title="查看任务" onclick="detail(@item.TaskId);"/>
                 @{
                            var data = new ViewDataDictionary();
                            data.Add("Step", step);
                            data.Add("Auth", auth);
                            Html.RenderPartial("Operation", item, data);
                     }
                </td>
            </tr>
            }
        }
        </tbody>
    </table>
    @{
        Html.RenderPartial("_Pager");
        }
 </div>
 <script src="/skin/datepicker/WdatePicker.js" type="text/javascript"></script>
 <script type="text/javascript">
     function loadpage() {
         var start = $("#txtStartDate").val();
         var end = $("#txtEndDate").val();
         var status = $("#ddlStatus").val();
         var page = $("#hidPageIndex").val();
         var code = $("#ddlCode").val();
         var old = $("#oldtask").attr("checked") == "checked";
         if (!page) page = 1;
         url = "/task/index?page=" + page;
         if (code) {
             url += "&code=" + code;
         }
         if (start) {
             url += "&start=" + start;
         }
         if (end) {
             url += "&end=" + end;
         }
         if (status) {
             url += "&status=" + status;
         }
         if (old) {
             url += "&old=1";
         }
         window.location = url;
     }
     function detail(id) {
         windowShowModalDialog("/task/details?id=" + id, window, 900, 550)
     }
     function showmarketing() {
         window.location = "/marketing/index?status=1";
     }
//     function processAll(type) {
//         var ids = getAllCheck(obj);
//         if (!ids) {
//             alert("请选择处理项。");
//             return;
//         }
//         windowShowModalDialog("/task/process?next=" + type + "&code=&id=" + ids, window, 650, 420)
//     }

//     function handoutAll(obj) {
//         var ids = getAllCheck(obj);
//         if (!ids) {
//             alert("请选择处理项。");
//             return;
//         }
//         windowShowModalDialog("/task/process?next=&code=handout&id=" + ids, window, 650, 420)
//     }
     function handout(id) {
         windowShowModalDialog("/task/next?id=" + id, window, 650, 420)
     }
     function commit(id) {
         windowShowModalDialog("/task/next?id=" + id, window, 650, 420)
     }
     function process(id) {
         windowShowModalDialog("/task/process?id=" + id, window, 650, 420)
     }
     function marketing(tid, did) {
         windowShowModalDialog("/marketing/create?tid=" + tid + "&did=" + did, window, 650, 580)
     }
</script>

